package control;

import utils.DataFormat;
import interfaces.CommandLine;


//@author A0098891H
public class Alert implements CommandLine{
	//check with budget manager
	private boolean _isBalanceCheckOn;
	private boolean _isDailyBudgetCheckOn;
	private boolean _isWeeklyBudgetCheckOn;
	private boolean _isMonthlyBudgetCheckOn;
	private boolean _isYearlyBudgetCheckOn;
	//check with Income manager
	private boolean _isExpenseExceededIncomeCheckOn;
	
	private ExpenseManager _xpnMan;
	private IncomeManager _incomeMan;
	private BudgetManager _budgetMan;
	
	private String _msg;
	private StringBuilder _alertMsg;
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public String getAlertMsg()
	{
		if(!check())
			return "";
		return _alertMsg.toString();
	}
	
	/**
	 * @param budgetMan
	 * @param xpnMan
	 * @param incomeMan
	 */
	public Alert(BudgetManager budgetMan, ExpenseManager xpnMan, IncomeManager incomeMan)
	{
		_msg = "";
		_xpnMan = xpnMan;
		_incomeMan = incomeMan;
		_budgetMan = budgetMan;
		
		_isBalanceCheckOn = false;
		_isDailyBudgetCheckOn = false;
		_isWeeklyBudgetCheckOn = false;
		_isMonthlyBudgetCheckOn = false;
		_isYearlyBudgetCheckOn = false;
		_isExpenseExceededIncomeCheckOn = false;
	}
	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		if(_isBalanceCheckOn)
			sb.append("  Balance Alert: On\n");
		else
			sb.append("  Balance Alert: Off\n");
		if(_isDailyBudgetCheckOn)
			sb.append("  Daily Budget Alert: On\n");
		else
			sb.append("  Daily Budget Alert: Off\n");
		if(_isWeeklyBudgetCheckOn)
			sb.append("  Weekly Budget Alert: On\n");
		else
			sb.append("  Weekly Budget Alert: Off\n");
		if(_isMonthlyBudgetCheckOn)
			sb.append("  Monthly Budget Alert: On\n");
		else
			sb.append("  Monthly Budget Alert: Off\n");
		if(_isYearlyBudgetCheckOn)
			sb.append("  Yearly Budget Alert: On\n");
		else
			sb.append("  Yearly Budget Alert: Off\n");
		if(_isExpenseExceededIncomeCheckOn)
			sb.append("  Expenditure Alert: On\n");
		else
			sb.append("  Expenditure Alert: Off\n");
		return sb.toString();
	}
	
	//false means no alert
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkBalance()
	{
		if(!_isBalanceCheckOn)
			return false;
		if(_budgetMan.get_totalBalance()<0)
		{
			_msg = "Your current balance is below 0.\n   Please update your income. \n";
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkDailyBudget()
	{
		if(!_isDailyBudgetCheckOn)
			return false;
		if(_budgetMan.get_dailyBudget().get_amount()<0)
		{
			_msg = "You have exceeded today's budget.\n    (Your today's budget available is "+DataFormat.returnTwoDecimals(_budgetMan.get_dailyBudget().get_amount()) + ")\n";
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkWeeklyBudget()
	{
		if(!_isWeeklyBudgetCheckOn)
			return false;
		if(_budgetMan.get_weeklyBudget().get_amount()<0)
		{
			_msg = "You have exceeded this week's budget.\n    (Your current week's budget available is "+DataFormat.returnTwoDecimals(_budgetMan.get_weeklyBudget().get_amount()) + ")\n";
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkMonthlyBudget()
	{
		if(!_isMonthlyBudgetCheckOn)
			return false;
		if(_budgetMan.get_monthlyBudget().get_amount()<0)
		{
			_msg = "You have exceeded this month's budget.\n    (Your current month's budget available is "+DataFormat.returnTwoDecimals(_budgetMan.get_monthlyBudget().get_amount()) + ")\n";
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkYearlyBudget()
	{
		if(!_isYearlyBudgetCheckOn)
			return false;
		if(_budgetMan.get_yearlyBudget().get_amount()<0)
		{
			_msg = "You have exceeded this year's budget.\n    (Your current year's budget available is "+DataFormat.returnTwoDecimals(_budgetMan.get_yearlyBudget().get_amount()) + "\n";
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean checkExpenseWithIncome()
	{
		if(!_isExpenseExceededIncomeCheckOn)
		{
			return false;
		}
		if(_xpnMan.getTotalAmountOfAll() > _incomeMan.getTotalAmountOfAll())
		{
			_msg = "Your total expenditures have exceeded your total income.\n";
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean check()
	{
		boolean hasAlert = false;
		_alertMsg = new StringBuilder("Alert!!!\n");
		if(checkBalance())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		if(checkDailyBudget())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		if(checkWeeklyBudget())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		if(checkMonthlyBudget())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		if(checkYearlyBudget())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		if(checkExpenseWithIncome())
		{
			hasAlert = true;
			_alertMsg.append(_msg);
		}
		return hasAlert;
	}


	//getters and setters
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setBalanceCheck(boolean flag)
	{
		_isBalanceCheckOn = flag;
	}
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setDailyBudgetCheck(boolean flag)
	{
		_isDailyBudgetCheckOn = flag;
	}
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setWeeklyBudgetCheck(boolean flag)
	{
		_isWeeklyBudgetCheckOn = flag;
	}
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setMonthlyBudgetCheck(boolean flag)
	{
		_isMonthlyBudgetCheckOn = flag;
	}
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setYearlyBudgetCheck(boolean flag)
	{
		_isYearlyBudgetCheckOn = flag;
	}
	/**
	 * 
	 * @param flag
	 * @Precondition 
	 * @Postcondition 
	 */
	public void setExpenseExceededIncomeCheck(boolean flag)
	{
		_isExpenseExceededIncomeCheckOn = flag;
	}
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getBalanceCheck()
	{
		return _isBalanceCheckOn;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getDailyBudgetCheck()
	{
		return _isDailyBudgetCheckOn;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getWeeklyBudgetCheck()
	{
		return _isWeeklyBudgetCheckOn;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getMonthlyBudgetCheck()
	{
		return _isMonthlyBudgetCheckOn;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getYearlyBudgetCheck()
	{
		return _isYearlyBudgetCheckOn;
	}
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean getExpenseExceededIncomeCheck()
	{
		return _isExpenseExceededIncomeCheckOn;
	}

	@Override
	public int readCmd(String cmd) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	
	
}
